const {
  SyncHook,
  SyncBailHook,
  AsyncParallelHook,
  AsyncSeriesHook
} = require('tapable')
// SyncHook 同步
// SyncBailHook 同步 回调停止
// AsyncParallelHook 异步 并行
// AsyncSeriesHook 异步 并行
class Lesson{
  constructor(){
    this.hooks = {
      go : new SyncBailHook(['address']),
      leave:new AsyncParallelHook(['name','age'])
    }
  }
  tap() {
    // 向hooks中注册事件，添加回调
    this.hooks.go.tap('111',(address)=>{
      console.log('111',address)
      // return 111 SyncBailHook 有返回值就停止
    })
    this.hooks.go.tap('222',(address)=>{
      console.log('222',address)
    })
    this.hooks.leave.tapAsync('tapAsync',(name,age,cb)=>{    
      setTimeout(() => {
        console.log('tapAsync',name,age)
        cb()
      }, 2000);
    })
    this.hooks.leave.tapPromise('tapPromise',(name,age)=>{    
      return new Promise(resolve=>{
        setTimeout(() => {
          console.log('tapPromise',name,age)
          resolve()
        }, 1000);
      })
    })
  }
  start() {
    //触发hooks
    this.hooks.go.call('new')
    this.hooks.leave.callAsync('zs',18,function(){
      console.log('end')
    })
  }
}

const l = new Lesson()

l.tap()
l.start()